package com.wanxi.util;

import com.wanxi.dto.ResultDTO;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : 林子淳
 * @create 2023/3/10 10:25
 */
@ControllerAdvice
public class BaseExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultDTO error(Exception e) {
        e.printStackTrace();
        System.out.println("调用了公共异常处理类");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long stime =System.currentTimeMillis();
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter("E:\\wanxi_java_project\\six-stage\\healthcare04_0222\\exception.txt",true));
            bufferedWriter.write(df.format(stime)+"调用了公共异常处理类报错："+e.getMessage());
            bufferedWriter.newLine();
        } catch (IOException ex) {
            ex.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return ResultDTO.getResult(e.getMessage());
    }
}
